package com.koron.css2.serviceManage.impl;

import com.github.pagehelper.PageHelper;
import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.PrintTemplateBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.PrintTemplateMapper;
import com.koron.util.Constant;
import com.koron.util.DBSourceUtils;
import com.koron.util.PrintInterfaceImpl;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 打印样式设计页面列表打印，此为打印示例程序，无实现用途
 * @author admin
 *
 */
public class PrintTemplateListPrint implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {

		@SuppressWarnings("rawtypes")
		MessageBean<String> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", String.class);

		try {
			//查询出测试用打印样式数据
			PrintTemplateMapper mapper = factory.getMapper(PrintTemplateMapper.class,DBSourceUtils.getDbEnv(req.getData()+""));
			PageHelper.startPage(1, 20); //默认只查20条测试数据
			List<PrintTemplateBean> list = mapper.listTemplate();
			//把打印样式写入打印缓存中
			ArrayList<HashMap<String,String>> data = new ArrayList<>();
			for (PrintTemplateBean tb : list) {
				HashMap<String,String> m = new HashMap<>();
				m.put("name", tb.getName());
				m.put("default_flag", tb.getDefaultFlag()==1?"是":"否");
				m.put("remark", tb.getRemark());
				m.put("scope", tb.getScope());
				
				data.add(m);
			}
			String key  = PrintInterfaceImpl.setPrintData(data);
			info.setData(key);
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		return info;
	}

}
